#include <QWidget>
#include <QMenu>
#include <QMessageBox>
#include <QDir>
#include <QFileInfo>
#include <QLibrary>
#include <QMainWindow>
#include <QApplication>
#include <QTextStream>
#include <pluginGl.h>
#include <qsciscintilla.h>
#include <Scintilla.h>
#include "NddPluginBridge.h"
#include "plugin_outline.h"

extern NddPluginBridge* pBridge;
extern std::function<QsciScintilla* (QWidget*)> s_getCurEdit;


void customMessageHandler(QtMsgType type, const QMessageLogContext &ctx, const QString &str)
{
    Q_UNUSED(type)
    Q_UNUSED(ctx)

    QString txt = str;
    QString app_run_addr;
    QString current_PathName = QCoreApplication::applicationDirPath();
    if (QFile::exists(current_PathName) == false) {
        app_run_addr = "plugin/outline_debug.log";
    } else {
        app_run_addr = current_PathName + "/" + "plugin/outline_debug.log";
    }
    QFile outFile(app_run_addr);
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    ts << txt << "\r\n";
}


NddPluginBridge::NddPluginBridge(QWidget* pNotepad, QMenu *pMenu, const QString& plgPath)
{
    m_pNotepad = pNotepad;
    m_outlineDock = nullptr;

    //qInstallMessageHandler(customMessageHandler);

    auto main_Window = dynamic_cast<QMainWindow*>(m_pNotepad);
    if (main_Window) {
        m_outlineDock = new OutlineDock(plgPath, "Hell Outline", main_Window);
        m_outlineDock->setObjectName("outline");
        m_outlineDock->setWidget(new PluginOutline(m_outlineDock, plgPath));
        m_outlineDock->setWindowTitle("Outline");
        pMenu->addAction(QString("Show"), [=, this]() {
            m_outlineDock->set_config("show", "true");
            m_outlineDock->show();
            PluginOutline* p_outline = dynamic_cast<PluginOutline*>(m_outlineDock->widget());
            p_outline->reflash_outline();
        });

        main_Window->addDockWidget(Qt::RightDockWidgetArea, m_outlineDock);

        connect(main_Window, SIGNAL(windowTitleChanged(const QString &)),
                        this, SLOT(slot_windowTitleChanged(const QString &)));

        QTimer::singleShot(0, qApp, [this]() {
            QString show = m_outlineDock->get_config("show");
            if(show.contains("false")) {
                m_outlineDock->hide();
            } else {
                QTimer::singleShot(1000, qApp, [this]() {
                    PluginOutline* p_outline = dynamic_cast<PluginOutline*>(m_outlineDock->widget());
                    p_outline->reflash_outline();
                });
            }
        });
    }
}

NddPluginBridge::~NddPluginBridge()
{

}

void NddPluginBridge::slot_windowTitleChanged(const QString &title)
{
    Q_UNUSED(title)

    if(!m_outlineDock->isHidden()) {
        if(pBridge->m_outlineDock) {
            PluginOutline* p_outline = dynamic_cast<PluginOutline*>(pBridge->m_outlineDock->widget());
            p_outline->reflash_outline();
        }
    }
}



